<?php

/**
 * This is the model class for table "user_has_template".
 *
 * The followings are the available columns in table 'user_has_template':
 * @property string $usr_code
 * @property integer $tem_code
 * @property integer $uht_code
 * @property string $uht_date
 *
 * The followings are the available model relations:
 * @property User $usrCode
 * @property Template $temCode
 */
class UserHasTemplate extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @return UserHasTemplate the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'user_has_template';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('usr_code, tem_code', 'required'),
			array('tem_code', 'numerical', 'integerOnly'=>true),
			array('usr_code', 'length', 'max'=>10),
			array('uht_date', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('usr_code, tem_code, uht_code, uht_date', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'usrCode' => array(self::BELONGS_TO, 'User', 'usr_code'),
			'temCode' => array(self::BELONGS_TO, 'Template', 'tem_code'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'usr_code' => 'Usr Code',
			'tem_code' => 'Tem Code',
			'uht_code' => 'Uht Code',
			'uht_date' => 'Uht Date',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('usr_code',$this->usr_code,true);
		$criteria->compare('tem_code',$this->tem_code);
		$criteria->compare('uht_code',$this->uht_code);
		$criteria->compare('uht_date',$this->uht_date,true);
		$criteria->condition = 'usr_code='.Yii::app()->user->id.' AND uth_state="activo"';
		

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
	
	public function findModel()
	{
    $model=$this::model()->findByAttributes(array('usr_code'=>$this->usr_code,'tem_code'=>$this->tem_code));
    if($model){
      
            $model->uht_date=date('Y-m-d H:i:s');      
    }
    return $model;
  }
}